/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package engine.entities.inventory.item;

import engine.entities.inventory.ContainerInventory;
import engine.entities.inventory.Inventory;
import engine.resource.Resources;

/**
 *
 * @author NathanT
 */
public class ContainerInventoryTable extends InventoryTable {
    
    public ContainerInventoryTable(ContainerInventory inventory) {
        super(Inventory.CONTAINER_INVENTORY_ROWS, Inventory.CONTAINER_INVENTORY_COLUMNS);
        this.setInventory(inventory);
        this.setInventoryTableListener(new ContainerInventoryListener());
    }

    @Override
    protected void initRenderers() {
        for(int i = 0; i < items.length; i++) {
            for (int j = 0; j < items[0].length; j++) {
                items[i][j] = new InventoryItemRenderer(null, Resources.getSprite("EMPTY_SLOT_ICON"), null);
            }
        }
    }
    
    public final void setInventory(ContainerInventory inventory) {
        this.inventory = inventory;
        for (int i = 0; i < items.length; i++) {
            for (int j = 0; j < items[0].length; j++) {
                if (inventory != null) {
                    items[i][j].setInvItem(inventory.getItem(i, j));
                }
                else {
                    items[i][j].setInvItem(null);
                }
            }
        }
    }
    
    private class ContainerInventoryListener implements InventoryTableListener {
        
        @Override
        public void itemChanged(int row, int column, InventoryItem newItem) {
            inventory.setItem(row, column, newItem);
        }
        
    }
    
}
